package com.chen.palmar.common.datasource;

import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.chen.palmar.common.override.HttpSubscriber;
import com.chen.palmar.common.widget.dialog.EditTextDialog;
import com.chen.palmar.entity.CollectEntity;
import com.chen.palmar.entity.GoodEntity;
import com.chen.palmar.entity.ShareEntity;
import com.primb.androidlibs.net.entity.HttpResultEntity;
import com.primb.androidlibs.net.exception.ApiException;
import com.primb.androidlibs.utils.Utils;

import java.util.HashMap;

import rx.Observable;
import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by chen on 2018/1/17.
 */

public class CommentRequest {

    public static CompositeSubscription subscription = new CompositeSubscription();

    public static void commentInfo(Context context, FragmentManager manger, String id, String type, OnLoadSuccessListener listener) {
        EditTextDialog dialog = new EditTextDialog();
        dialog.setListener(v -> {
            if (!TextUtils.isEmpty(dialog.getText())) {
                dialog.dismiss();
                InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.toggleSoftInput(0, 0);
                }
                commentInfo(id, type, dialog.getText(), listener);
            } else {
                Toast.makeText(context, "请输入评论内容", Toast.LENGTH_SHORT).show();
            }
        });
        dialog.show(manger);
    }

    /**
     * 收藏
     *
     * @param isCancel
     * @param type     收藏类型1、消息 2、经销商 3、商品 4、店铺
     */
    public static void collectInfo(String id, String type, boolean isCancel, OnLoadSuccessListener listener) {
        HashMap<String, String> map = new HashMap<>();
        map.put("ref_id", id);
        map.put("ref_type", type);//消息
        Observable<CollectEntity> observable = !isCancel ? DataCenter.collectInfo(map) : DataCenter.collectCancelInfo(map);
        Subscription subscribe = observable.subscribe(new HttpSubscriber<CollectEntity>(Utils.getApp()) {
            @Override
            public void onNext(CollectEntity collectEntity) {
                listener.onSuccess(collectEntity);
            }

            @Override
            public void onError(ApiException e) {
                super.onError(e);
                listener.onFailure(e);
            }
        });
        subscription.add(subscribe);
    }

    /**
     * 评论
     *
     * @param text
     * @param listener
     */
    private static void commentInfo(String id, String type, String text, OnLoadSuccessListener listener) {
        HashMap<String, String> map = new HashMap<>();
        map.put("ref_id", id);
        map.put("ref_type", type);
        map.put("content", text);
        Subscription subscribe = DataCenter.commentInfo(map).subscribe(new HttpSubscriber<HttpResultEntity>(Utils.getApp()) {
            @Override
            public void onNext(HttpResultEntity entity) {
                listener.onSuccess(entity);
            }

            @Override
            public void onError(ApiException e) {
                super.onError(e);
                listener.onFailure(e);
            }
        });
        subscription.add(subscribe);
    }

    /**
     * 点赞
     */
    public static void goodInfo(String id, OnLoadSuccessListener listener) {
        HashMap<String, String> map = new HashMap<>();
        map.put("article_id", id);
        Subscription subscribe = DataCenter.goodInfo(map).subscribe(new HttpSubscriber<GoodEntity>(Utils.getApp()) {
            @Override
            public void onNext(GoodEntity entity) {
                listener.onSuccess(entity);
            }

            @Override
            public void onError(ApiException e) {
                super.onError(e);
                listener.onFailure(e);
            }
        });
        if (subscription.isUnsubscribed()) {
            subscription.clear();
        }
        subscription.add(subscribe);
    }


    public static void shareContent(String id, String type, OnLoadSuccessListener listener) {
        HashMap<String, String> map = new HashMap<>();
        map.put("ref_id", id);//1、商品  2、动态 3、公司 4、门店
        map.put("ref_type", type);
        Subscription subscribe = DataCenter.shareContent(map).subscribe(new HttpSubscriber<ShareEntity>(Utils.getApp()) {
            @Override
            public void onNext(ShareEntity entity) {
                listener.onSuccess(entity);
            }

            @Override
            public void onError(ApiException e) {
//                super.onError(e);
                listener.onFailure(e);
            }
        });
        if (subscription.isUnsubscribed()) {
            subscription.clear();
        }
        subscription.add(subscribe);
    }

    public static void cancelRequest() {
        subscription.clear();
    }


    public interface OnLoadSuccessListener<T> {
        void onSuccess(T t);

        void onFailure(ApiException e);
    }
}
